﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using System.Windows.Interactivity;
using Eaglight.Behaviors;
using ESRI.ArcGIS.Client.Tasks;
using Eaglight.Extentions;
using LightControls;

namespace Eaglight
{
    public class MappingToolbar : Toolbar
    {
        private LcImagePushRadioButton btnCursor;
        private LcImagePushRadioButton btnZoomIn;
        private LcImagePushRadioButton btnZoomOut;
        private LcImagePushRadioButton btnPan;
        private LcImagePushRadioButton btnIdentify;
        private LcImageButton btnFullExtent;
        private ZoomOutBehavior zo = new ZoomOutBehavior();
        private ZoomInBehavior zi = new ZoomInBehavior();
        private IdentifyBehavior io = new IdentifyBehavior();


        public event EventHandler<IdentifyEventArgs> OnIdentifyCompleted;
        public event EventHandler<TaskFailedEventArgs> OnIdentifyFailed; 

        public static readonly DependencyProperty ShowIdentifyButtonProperty = DependencyProperty.Register("ShowIdentifyButton", typeof(bool), typeof(MappingToolbar), null);
        public static readonly DependencyProperty AutoIdentifyProperty = DependencyProperty.Register("AutoIdentify", typeof(bool), typeof(MappingToolbar), null);
        public static readonly DependencyProperty IdentifyResultsPanelProperty = DependencyProperty.Register("IdentifyResultsPanel", typeof(IdentifyResultsPanel), typeof(MappingToolbar), null);
        public static readonly DependencyProperty IdentifyLayerOptionProperty = DependencyProperty.Register("IdentifyLayerOptions", typeof(LayerOption), typeof(MappingToolbar), null);
        public static readonly DependencyProperty IdentifyLayerIdProperty = DependencyProperty.Register("IdentifyLayerId", typeof(string), typeof(MappingToolbar), null);
        public static readonly DependencyProperty MapProperty = DependencyProperty.Register("Map", typeof(Map), typeof(MappingToolbar), null);

        
        public MappingToolbar()
        {
            DefaultStyleKey = typeof(MappingToolbar);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            btnCursor = (LcImagePushRadioButton)base.GetTemplateChild("btnCursor");
            btnZoomIn = (LcImagePushRadioButton)base.GetTemplateChild("btnZoomIn");
            btnZoomOut = (LcImagePushRadioButton)base.GetTemplateChild("btnZoomOut");
            btnPan = (LcImagePushRadioButton)base.GetTemplateChild("btnPan");
            btnIdentify = (LcImagePushRadioButton)base.GetTemplateChild("btnIdentify");
            btnFullExtent = (LcImageButton)base.GetTemplateChild("btnFullExtent");
            controlsInitialized = true;

            if (ShowToolTips == true)
                enableToolTips();
            else
                disableToolTips();

            initEvents();
        }

        private void initEvents()
        {
            //btnCursor.Checked += new RoutedEventHandler(btnCursor_Checked);
            btnZoomIn.Checked += new RoutedEventHandler(btnZoomIn_Checked);
            btnZoomIn.Unchecked += new RoutedEventHandler(btnZoomIn_Unchecked);
            btnZoomOut.Checked += new RoutedEventHandler(btnZoomOut_Checked);
            btnZoomOut.Unchecked += new RoutedEventHandler(btnZoomOut_Unchecked);
            btnFullExtent.Click += new RoutedEventHandler(btnFullExtent_Click);
            btnIdentify.Checked +=new RoutedEventHandler(btnIdentify_Checked);
            btnIdentify.Unchecked += new RoutedEventHandler(btnIdentify_Unchecked);

            io.IdentifyCompleted += new EventHandler<ESRI.ArcGIS.Client.Tasks.IdentifyEventArgs>(io_IdentifyCompleted);
            io.IdentifyFailed += new EventHandler<ESRI.ArcGIS.Client.Tasks.TaskFailedEventArgs>(io_IdentifyFailed);
        }

        //void btnCursor_Checked(object sender, RoutedEventArgs e)
       // {
        //    Map.GetDrawObject().IsEnabled = false;
       // }

        void io_IdentifyFailed(object sender, ESRI.ArcGIS.Client.Tasks.TaskFailedEventArgs e)
        {
            if (OnIdentifyFailed != null)
                OnIdentifyFailed(sender, e);
        }

        void io_IdentifyCompleted(object sender, ESRI.ArcGIS.Client.Tasks.IdentifyEventArgs e)
        {
            if (OnIdentifyCompleted != null)
                OnIdentifyCompleted(sender, e);
            if (AutoIdentify)
            {
                if (this.IdentifyResultsPanel == null)
                    throw new Exception("No identify panel specified");
                else
                {
                    IdentifyResultsPanel.Visibility = Visibility.Visible;
                    IdentifyResultsPanel.UpdateLayout();
                    IdentifyResultsPanel.IdentifyResults = e.IdentifyResults;
                }
            }
        }

        void btnIdentify_Unchecked(object sender, RoutedEventArgs e)
        {
            io.Detach();
        }

        void btnIdentify_Checked(object sender, RoutedEventArgs e)
        {
            io.Attach(Map);
            io.IdentifyLayerId = IdentifyLayerId;
            io.IdentifyLayerOptions = IdentifyLayerOptions;
        }

        void btnZoomOut_Checked(object sender, RoutedEventArgs e)
        {
            zo.Attach(Map);
        }
        
        void btnZoomOut_Unchecked(object sender, RoutedEventArgs e)
        {
            zo.Detach();   
        }

        void btnZoomIn_Checked(object sender, RoutedEventArgs e)
        {
            zi.Attach(Map);
        }

        void btnZoomIn_Unchecked(object sender, RoutedEventArgs e)
        {
            zi.Detach();
        }

        void btnFullExtent_Click(object sender, RoutedEventArgs e)
        {
            Map.ZoomTo(Map.Layers.GetFullExtent());
        }

        protected override void enableToolTips()
        {
            if (controlsInitialized)
            {
                ToolTipService.SetToolTip(btnZoomIn, "Zoom In");
                ToolTipService.SetToolTip(btnZoomOut, "Zoom Out");
                ToolTipService.SetToolTip(btnPan, "Pan");
                ToolTipService.SetToolTip(btnFullExtent, "Full Extent");
                ToolTipService.SetToolTip(btnIdentify, "Identify");
            }
        }

        protected override void disableToolTips()
        {
            if (controlsInitialized)
            {
                ToolTipService.SetToolTip(btnZoomIn, null);
                ToolTipService.SetToolTip(btnZoomOut, null);
                ToolTipService.SetToolTip(btnPan, null);
                ToolTipService.SetToolTip(btnFullExtent, null);
                ToolTipService.SetToolTip(btnIdentify, null);
            }
        }

        public Map Map
        {
            get
            {
                return (Map)GetValue(MapProperty);
            }
            set
            {
                SetValue(MapProperty, value);
            }
        }


        public bool AutoIdentify
        {
            get
            {
                return (bool)GetValue(AutoIdentifyProperty);
            }

            set
            {
                SetValue(AutoIdentifyProperty, value);
            }

        }

        public IdentifyResultsPanel IdentifyResultsPanel
        {
            get
            {
                return (IdentifyResultsPanel)GetValue(IdentifyResultsPanelProperty);
            }

            set
            {
                SetValue(IdentifyResultsPanelProperty, value);
            }

        }

        public LayerOption IdentifyLayerOptions
        {
            get
            {
                return (LayerOption)GetValue(IdentifyLayerOptionProperty);
            }
            set
            {
                SetValue(IdentifyLayerOptionProperty, value);
            }
        }

        public string IdentifyLayerId
        {
            get
            {
                return (string)GetValue(IdentifyLayerIdProperty);
            }

            set
            {
                SetValue(IdentifyLayerIdProperty, value);
            }

        }
    }
}
